﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BusinessLogicLayer.Base;
using DataDefinition;
using DALLayer.Providers;
using Objects;

namespace BusinessLogicLayer.Manager
{
    public class ArticleTagManager:  ManagerBase<ArticleTagDataProvider>
    {
        #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="ArticleTagManager" /> class.
        /// </summary>
        public ArticleTagManager() { } 
        #endregion

        #region [Manager base class implementations]
        /// <summary>
        /// Pass the data provider to the class
        /// </summary>
        protected override void InitializeDataProvider()
        {
            //Pass the specific data provider to the manager class
            base.DataProvider = new ArticleTagDataProvider();
        }

        #endregion

        #region [Public Methods]
        /// <summary>
        /// Gets all the tag by article id
        /// </summary>
        /// <param name="articleID">articleID</param>
        /// <returns>true or false</returns>
        public List<GetTagByArticleID> GetTagByArticleID(int articleID)
        {
            //The tag details to be returned
            List<GetTagByArticleID> articleTagDetails = null;
            try
            {
                //Get the data set returned from the data provider
                AutoFixDataSet dataSet = base.DataProvider.GetArticleTags(articleID);

                if (dataSet != null && dataSet.GetTagArticleID != null && dataSet.GetTagArticleID.Rows != null)
                {
                    //Set the returned filled list
                    articleTagDetails = FillGetTagByArticleIDObjectsByEntities(dataSet.GetTagArticleID);
                }

            }
            catch (Exception eX)
            {
                // log the error here
            }
            //Return the article tag details list
            return articleTagDetails;

        }
        /// <summary>
        /// Update or insert an article tag.
        /// </summary>
        /// <param name="articleTag">article tag object</param>
        /// <returns>true or false</returns>
        public bool UpdateArticleTag(ArticleTag articleTag)
        {
            bool success = false;
            try
            {
                //if there is an id
                if (articleTag.ArticleID > 0 && articleTag.TagID > 0)
                {
                    //Update the article tag
                    success = base.DataProvider.InsertArticleTag(articleTag.ArticleID, articleTag.TagID, articleTag.UserID, articleTag.IsActive);
                }
                
            }
            catch (Exception eX)
            {
                // log the error here
            }
            //returns true or false
            return success;

        }

        #endregion

        #region [Private Methods]

        /// <summary>
        /// Fills the article tag details object
        /// </summary>
        /// <param name="dataTable">the data table</param>
        /// <returns>The article tag details list</returns>
        private List<GetTagByArticleID> FillGetTagByArticleIDObjectsByEntities(AutoFixDataSet.GetTagArticleIDDataTable dataTable)
        {
            //The article tag list that contains the objects
            //Loop through the data table and create new ArticleTagDetailsObject 
            //finally select the ArticleTagDetailsObject and convert to list
            List<GetTagByArticleID> articleTagDetails = (from atdo in
                                                             (from atd in dataTable.AsEnumerable<AutoFixDataSet.GetTagArticleIDRow>()
                                                              select new
                                                              {
                                                                  ArticleTagDetailsObject = new GetTagByArticleID()
                                                                  {
                                                                      Tag = atd.Tag
                                                                  }
                                                              })
                                                         select atdo.ArticleTagDetailsObject).ToList();
            //Return the filled article tag details list
            return articleTagDetails;
        }
        #endregion
    }
}
